package net.lateeye.search.thesissearch.acm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.SQLException;
import java.util.InvalidPropertiesFormatException;
import java.util.LinkedList;
import java.util.Properties;

import net.lateeye.search.AbsResultSet;
import net.lateeye.search.AbsSearchEngineClient;
import net.lateeye.search.AbsSearchResultPageTextProcessor;
import net.lateeye.search.thesissearch.ThesisSearchProp;
import net.lateeye.util.NoValueSpecifiedException;

/**
 * Provide access to ACM Digital Library (http://portal.acm.org/).
 * 
 * @author Isao Isaac Saito <130s@lateeye.net>
 * @since May 2010
 */
public class AcmClient extends AbsSearchEngineClient
{
	public AcmClient(ThesisSearchProp props)
	{
		super(props);
	}

	@Override
	protected String buildDefaultSearchConditions()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String createSearchQueryUrl(String encodedQueryStr,
			int requiredResultSize, int startPos, String searchConditions)
	{
		String prefix = "";
		String suffix = "";
		String query_startPos = "";
		String query_prefix = "";
		String query_suffix = "";

		if (this.props.getProperty(AcmClientProps.PROPKEY_ACM_URL_PREFIX) != null) {
			prefix = this.props
					.getProperty(AcmClientProps.PROPKEY_ACM_URL_PREFIX);
		}
		if (this.props.getProperty(AcmClientProps.PROPKEY_ACM_URL_PREFIX) != null) {
			suffix = this.props
					.getProperty(AcmClientProps.PROPKEY_ACM_URL_SUFFIX);
		}
		if (this.props.getProperty(AcmClientProps.PROPKEY_ACM_URL_START) != null) {
			query_startPos = this.props
					.getProperty(AcmClientProps.PROPKEY_ACM_URL_START);
		}
		try {
			query_prefix = this.props
					.getProperty(AcmClientProps.PROPKEY_ACM_URL_QUERY_PREFIX);
		} catch (NoValueSpecifiedException e) {
			System.err.println(AcmClientProps.PROPKEY_ACM_URL_QUERY_PREFIX
					+ " missing.");
		}
		try {
			query_suffix = this.props
					.getProperty(AcmClientProps.PROPKEY_ACM_URL_QUERY_SUFFIX);
		} catch (NoValueSpecifiedException e) {
			System.err.println(AcmClientProps.PROPKEY_ACM_URL_QUERY_SUFFIX
					+ " missing.");
		}

		String qUrl = prefix + query_prefix + encodedQueryStr + query_suffix
				+ suffix + query_startPos + (startPos + 1);

		System.out.println(super.getClass().getName()
				+ "#createSearchQueryUrl \n\t" + qUrl);

		return qUrl;
	}

	@Override
	public String doGetHtmlFile(String queryStr) throws IOException
	{
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @see net.lateeye.search.AbsSearchEngineClient#doGetResultSetForSinglePageOfSearchResult(java.lang.String,
	 *      net.lateeye.search.AbsSearchResultPageTextProcessor)
	 * @return AbsResultSet is actually AcmResultset
	 */
	@Override
	public AbsResultSet doGetResultSetForSinglePageOfSearchResult(
			String queryStr, AbsSearchResultPageTextProcessor analyzer)
			throws IOException
	{
		AcmResultpageProcessor anlyz = (AcmResultpageProcessor) analyzer;

		StringBuilder result = new StringBuilder();
		try {
			URLConnection conn = new URL(queryStr).openConnection();
			BufferedReader br = new BufferedReader(new InputStreamReader(conn
					.getInputStream(), CHARSET_UTF));
			String line = null;
			// int c = 0;
			while ((line = br.readLine()) != null) {
				result.append(line);
				// System.out.println("Debug: line = " + c + " " + line);
				// c++;
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String htm = result.toString();
		AcmResultSet rset = (AcmResultSet) anlyz.createResultSet(queryStr, htm);
		return rset;
	}

	@Override
	/*
	 * @deprecated 1/13/11 Hasn't been implemented.
	 */
	public boolean init(Properties param)
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected AbsSearchResultPageTextProcessor getAnalyzer()
	{
		return new AcmResultpageProcessor();
	}

	public static void main(String args[])
	{
		String confPath = null;
		String query = null;

		AcmClient gc = null;
		try {
			confPath = args[0];
			query = args[1];
			gc = new AcmClient(new AcmClientPropsImpl(
			// "conf/Default_AcmOrg.properties"));
					confPath));
		} catch (InvalidPropertiesFormatException e1) {
			e1.printStackTrace();
			System.exit(1);
		}
		AcmResultSet grs = null;
		try {
			grs = (AcmResultSet) gc.executeQuery(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.print("size: " + grs.size());
	}

	/*
	 * @deprecated 1/13/11 Hasn't been implemented.
	 */
	public boolean isClosed() throws SQLException
	{
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * @deprecated 1/13/11 Hasn't been implemented.
	 */
	public boolean isPoolable() throws SQLException
	{
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * @deprecated 1/13/11 Hasn't been implemented.
	 */
	public void setPoolable(boolean arg0) throws SQLException
	{
		// TODO Auto-generated method stub

	}

	/*
	 * @deprecated 1/13/11 Hasn't been implemented.
	 */
	public boolean isWrapperFor(Class<?> arg0) throws SQLException
	{
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * @deprecated 1/13/11 Hasn't been implemented.
	 */
	public <T> T unwrap(Class<T> arg0) throws SQLException
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	/*
	 * @deprecated 1/13/11 Hasn't been implemented.
	 */
	protected LinkedList<String> extractDocumentURLs(String body)
	{
		// TODO Auto-generated method stub
		return null;
	}
}